<template>
	<view class="select-address">
		<u-picker :show="show" ref="addressPicker" :columns="addressList" :defaultIndex="[2,2,2]"
			:closeOnClickOverlay="true" @change="addressChange" @cancel="cancel" @close="close" confirmColor="#FF2D71"
			@confirm="confirm">
		</u-picker>
	</view>
</template>
<script>
	export default {
		name: "select-address",
		data() {
			return {
				// 地址列表
				addressList: [
					[],
					[],
					[]
				],
				addressData: [],
				// 地址选择下标
				addressValue: [0, 0, 0],
			};
		},
		props: { // 此处定义传入的参数
			// 是否显示
			show: {
				type: Boolean,
				default: true,
			},
		},
		watch: {},
		mounted() {},
		methods: {
			getAddressList(defaultAddress = {}) {
				this.$get("area/index").then(res => {
					this.addressData = res.data
					this.getAddress(defaultAddress)
				}).catch(ex => {
					console.log(ex)
					this.$errorToast();
				});
			},
			/**
			 * @desc 获取省市区地址
			 * @param defaultAddress 默认地址
			 * */
			getAddress(defaultAddress = {}) {
				let type = 0; // 0-不默认选中
				if (defaultAddress.province != undefined && defaultAddress.province != '') {
					type = 1; // 1-默认选中
				}
				let province = [],
					city = [],
					area = [];
				// 获取省份
				this.addressData.forEach((item, index) => {
					province.push(item.area_name);
					// if (type == 1 && defaultAddress.province == item.area_name) {
					// 	this.addressValue[0] = index
					// }
				})
				// 获取城市
				this.addressData[this.addressValue[0]].child.forEach((item, index) => {
					city.push(item.area_name);
					// if (type == 1 && defaultAddress.city == item.area_name) {
					// 	this.addressValue[1] = index
					// }
				})
				// 获取区县
				this.addressData[this.addressValue[0]].child[this.addressValue[1]].child.forEach((item, index) => {
					area.push(item.area_name);
					// if (type == 1 && defaultAddress.area == item.area_name) {
					// 	this.addressValue[2] = index
					// }
				})
				if (type == 1) {
					this.$refs.addressPicker.setIndexs(this.addressValue, [0, 0, 0])
				}
				// 重新设置对应列的选择值
				this.addressList = [province, city, area]
			},
			// 地址选择
			addressChange(e) {
				if (e.columnIndex == 0) { // 当前选择省份
					this.addressValue[0] = e.indexs[0]
					this.addressValue[1] = 0 // 城市选中第一个 
				} else if (e.columnIndex == 1) { // 当前选择城市
					this.addressValue[1] = e.indexs[1]
					this.addressValue[2] = 0 // 区县选中第一个 
				} else { // 当前选择区县
					this.addressValue[2] = e.indexs[2]
				}
				// 重新设置对应列的选择值
				this.$refs.addressPicker.setIndexs(this.addressValue, [0, 0, 0])
				this.getAddress();
			},
			// 地址确定
			confirm(e) {
				let params = {
					arr: e,
					province_id: this.addressData[e.indexs[0]].area_id,
					city_id: this.addressData[e.indexs[0]].child[e.indexs[1]].area_id,
					area_id: this.addressData[e.indexs[0]].child[e.indexs[1]].child[e.indexs[2]].area_id
				}
				this.$emit('confirm', params)
			},
			cancel() {
				this.$emit('close')
			},
			close() {
				this.$emit('close')
			},
		},
	}
</script>
<style lang="scss">
	.page-main {
		width: 100%;
	}

	.fixed {}

	.fullScreen {
		height: 100vh !important;
		background-color: white;
		opacity: 0.6;
	}

	.zoom-fixed {
		position: absolute;
		top: 0;
		left: 0;
		z-index: 100;
	}

	.zoom {}
</style>